<!DOCTYPE HTML>
<html>
<head>
    {include file="common:meta"}
</head>
<body>

<div class="page-container">
    {foreach $gold_egg as $key=>$value}
    <div class="ibox-content" id="toolbar_{$value.id}">

        <button type="button" class="btn btn-primary setLoop" data-flag="{$value.id}">{$value.egg_name} 控制轮数</button>
        <span class="c-red"><cite>&nbsp;&nbsp;&nbsp;</cite></span>

        <span class="c-red"><cite>{$value.egg_name} 已追加次数:{$num[$value.id] -1}</cite></span>

        <span class="c-red"><cite>&nbsp;&nbsp;&nbsp;</cite></span>
        <!--<button type="button" onclick="location.replace(location.href);" class="btn btn-success ">刷新</button>-->
        <br>

        {if	($stop_type	==	1)}
        <span class="c-red"><cite>&nbsp;&nbsp;&nbsp;</cite></span>

        <span class="c-red"><cite>当前宝箱状态: 未停服</cite></span>

        <span class="c-red"><cite>&nbsp;&nbsp;&nbsp;</cite></span>

        <button type="button" class="btn btn-danger stopOpen"  data-type-now="2">停服2分钟</button>

        <span class="c-red"><cite>&nbsp;&nbsp;&nbsp;</cite></span>
        {else	/}
        <span class="c-red"><cite>&nbsp;&nbsp;&nbsp;</cite></span>

        <span class="c-red"><cite>当前宝箱状态: 已停服</cite></span>

        <span class="c-red"><cite>&nbsp;&nbsp;&nbsp;</cite></span>

        <button type="button" class="btn btn-danger stopOpen"  data-type-now="1">放开停服</button>

        <span class="c-red"><cite>&nbsp;&nbsp;&nbsp;</cite></span>
        {/if}
    </div>
    {/foreach}
    <div class=" float-e-margins">
        <div class="ibox-content">
            <div class="tabs-container">
                <ul class="nav nav-tabs">
                    {foreach $gold_egg as $key=>$value}
                    <li class="{if	($key	==	0)} active {/if}"><a data-toggle="tab" href="#tab-{$value.id}" aria-expanded="true">{$value.egg_name}赠送全服</a>
                    </li>
                    {/foreach}

                    <li class=""><a data-toggle="tab" href="#tab-2" aria-expanded="true">添加用户</a>
                    </li>
                </ul>
                <div class="tab-content">

                    {foreach $gold_egg as $key=>$value}
                    <div class=" tab-pane {if	($key	==	0)} active {/if}" id="tab-{$value.id}">
                        <table id="tb_departments_{$value.id}" class="gold_egg_id" data-flag="{$value.id}"></table>
                    </div>
                    {/foreach}

                    <div class=" tab-pane" id="tab-2">
                        <div class="ibox-content">
                            <form class="form-horizontal" id="form" data-url="/LoopUserDataAdd">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="u_id">
                                        <span class="c-red">*</span>用户UID：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <input type="text" class="form-control" id="u_id" name="u_id">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="total_num">
                                        <span class="c-red">*</span>设定抽奖次数：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <input type="number" class="form-control" id="total_num" name="total_num">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="total_num">
                                        <span class="c-red">*</span>适用宝箱：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <select class="form-control" name="gold_egg_id" id="gold_egg_id">
                                            {volist name='gold_egg_list' id='vo'}
                                            <option value="{$vo.id}">{$vo.egg_name}</option>
                                            {/volist}
                                        </select>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="gift_type">
                                        <span class="c-red">*</span>指定奖品类型：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <select class="form-control" name="gift_type" id="gift_type">
                                            <option value="1">礼物</option>
                                            <option value="3">道具</option>
                                        </select>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="id">
                                        <span class="c-red">*</span>奖品名：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <select class="form-control" name="id" id="id">
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                                        <button class="btn btn-primary" type="button" onclick="commonSubmit(this)">添加
                                        </button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="common:footer"}
<script>

    $(document).ready(function () {
        initTable();
        changeGift();
    });

    function initTable() {

        $(".gold_egg_id").each(function(){
            var name = $(this).attr('data-flag');
            $('#tb_departments_'+name).bootstrapTable({
                url: '/LoopHistoryData?gold_egg='+name,         //请求后台的URL（*）
                dataField: "data",
                method: 'get',                      //请求方式（*）
                striped: true,                      //是否显示行间隔色
                cache: true,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                pagination: true,                   //是否显示分页（*）
                queryParams: queryParams,//传递参数（*）
                showHeader: true,
                showToggle: true,
                showRefresh: true,
                sortStable: true,
                sortName: 'id',
                sortOrder: "asc",                   //排序方式
                sidePagination: "Client",           //分页方式：client客户端分页，server服务端分页（*）
                pageNumber: 1,                       //初始化加载第一页，默认第一页
                pageSize: 10,                       //每页的记录行数（*）
                pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
                showColumns: true,                  //是否显示所有的列
                minimumCountColumns: 2,             //最少允许的列数
                toolbar: '#toolbar_'+name,
                columns: [{
                    field: 'user_id',
                    title: '用户id',
                    align: 'center',
                    sortable: true
                }, {
                    field: 'user_nickname',
                    title: '用户名',
                    align: 'center'
                }, {
                    field: 'gold_egg_id',
                    title: '宝箱id',
                    align: 'center'
                },{
                    field: 'prize_name',
                    title: '奖品名称',
                    align: 'center'
                }, {
                    field: 'loop',
                    title: '指定次数',
                    align: 'center'
                }, {
                    field: 'loop_now',
                    title: '已抽次数',
                    align: 'center'
                }, {
                    field: 'status',
                    title: '状态',
                    align: 'center',
                    formatter: function (value, row, index) {

                        if(row.status == 1 ){
                            return "抽奖中";
                        }else if(row.status == 2){
                            return "已中奖";
                        }else if(row.status == 3){
                            return "已取消";
                        }
                    }
                },{
                    field: 'create_time',
                    title: '指定时间',
                    align: 'center',
                    sortable: true
                },{
                    field: 'update_time',
                    title: '操作时间',
                    align: 'center',
                    sortable: true
                },{
                    field: 'operator',
                    title: '操作人员',
                    align: 'center'
                },{
                    field: 'Button',
                    title: '操作',
                    events: operateEvents,
                    formatter: operateFormatter
                }]
            });
        });



    }

    //得到查询的参数

    function queryParams(params) {
        params.offset = params.offset / params.limit + 1;
        params.key = $('#key').val();
        params.value = $('#value').val();
        console.log(JSON.stringify(params));
        return params;
    }

    function operateFormatter(value, row, index) {
        if(row.status == 1){
            return [
                '<button type="button"  class="del-user btn btn-primary btn-sm" style="margin-right:15px;">取消</button>'
            ].join('');
        }else if(row.status == 2){
            return "已中奖";
        }else if(row.status == 3){
            return "已取消";
        }

    }

    window.operateEvents = {
        'click .del-user': function (e, value, row, index) {

            layer.confirm('取消后，将删除该用户的抽奖次数',{
                title:['提示','font-size:18px;font-family:楷体;font-weight:700;'],
                btn:['确定','取消']
            },function(){
                $.ajax({
                    type: 'get',
                    dataType: 'json',
                    url: '/LoopUserDataDel',
                    data: {'user_id': row.user_id,'gold_egg_id':row.gold_egg_id},
                    success: function (re) {
                        if (re.code === 100) {
                            layer.msg(re.msg, {icon: 1, time: 2000});
                            $('#tb_departments_'.row.gold_egg_id).bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
                        } else {
                            layer.msg(re.msg,{icon: 2, time: 2000});
                        }
                    },
                    error: function (data) {
                        console.log(data.msg);
                    }
                })
            });
        }


    };

    $('.setLoop').click(function(){
        var gold_egg = $(this).attr('data-flag');
        layer.open({
            type: 2,
            title: ['修改规则','font-size:18px;font-family:宋体;font-weight:700;'],
            shadeClose: true,
            shade: false,
            maxmin: true, //开启最大化最小化按钮
            area: ['800px', '600px'],
            content:"/LoopSetData?gold_id="+gold_egg,
            offset:'auto',
            scrollbar:false,
            end: function () {
                location.reload();
            }
        });
    });


    //停服按钮
    $('.stopOpen').click(function(){
        var open_box_type = $(this).attr("data-type-now");
        layer.confirm('确定要修改停服状态吗？',{
            title:['提示','font-size:18px;font-family:楷体;font-weight:700;'],
            btn: ['确定','取消'] //按钮
        },function(){
            $.ajax({
                type:'get',
                url:"/LoopToogle",
                data: {'open_box_type': open_box_type},
                async:true,
                dataType:'json',
                success:function(result){
                    if(result.code == 100){
                        layer.msg(result.msg,{
                            icon:1,
                            time:2000
                        });
                        location.reload();	// 很重要的一步，刷新url！
                    }
                    else{
                        layer.msg(result.msg,{
                            icon:2,
                            time:2000
                        });
                    }
                },
                error:function(err){
                    layer.msg('服务器开小差了！',{
                        time:1500
                    });

                    console.log(err);
                }
            });
        });
    });

    //切换模式按钮
    $('#toggle_type').click(function(){
        var open_box_type = $(this).attr("data-type-now");
        console.log(open_box_type);

        layer.confirm('确定要切换宝箱模式吗？',{
            title:['提示','font-size:18px;font-family:楷体;font-weight:700;'],
            btn: ['确定','取消'] //按钮
        },function(){
            $.ajax({
                type:'get',
                url:"/ToggleServer",
                async:true,
                data: {'open_box_type': open_box_type},
                dataType:'json',
                success:function(result){
                    if(result.code == 100){
                        layer.msg(result.msg,{
                            icon:1,
                            time:2000
                        });
                        location.reload();	// 很重要的一步，刷新url！
                    }
                    else{
                        layer.msg(result.msg,{
                            icon:2,
                            time:2000
                        });
                    }
                },
                error:function(err){
                    layer.msg('服务器开小差了！',{
                        time:1500
                    });

                    console.log(err);
                }
            });
        });

    });

    $('#gift_type').change(function () {
        changeGift();
    });
//根据指定类型更换奖品
    function changeGift(){
        var gift_type = $('#gift_type').val();
        $.ajax({
            type:'get',
            dataType:'json',
            url: '/changeGift',
            data: {'gift_type': gift_type},
            success: function (re) {
                console.log(re);
                var htmlText = "<option value='' selected>--请选择--</option>";

                for(var i in re){
                    htmlText+="<option value="+ re[i].id+">"+ re[i].gift_name+"</option>";
                }
                $('#id').html(htmlText);

            }
        });
    }

    // 搜索按钮触发事件
    $(function () {
        $("#eventQuery").click(function () {
            $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
        });

    });
</script>
</body>
</html>